package com.example.deepseek.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

/**
 * WebSocket配置类
 * 配置WebSocket消息代理和端点
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 配置消息代理前缀，客户端订阅时需要使用这些前缀
        registry.enableSimpleBroker("/topic/chat/", "/topic/stream/", "/topic/complete/", "/topic/session/");
        // 配置客户端发送消息的目标前缀
        registry.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册标准WebSocket端点
        registry.addEndpoint("/websocket")
                .setAllowedOriginPatterns("*")
                .withSockJS()
                .setSessionCookieNeeded(true)
                .setStreamBytesLimit(512 * 1024) // 512K
                .setHttpMessageCacheSize(1000)
                .setDisconnectDelay(30 * 1000);

        // 添加第二个端点，与前端当前使用的一致
        registry.addEndpoint("/websocket-chat")
                .setAllowedOriginPatterns("*")
                .withSockJS()
                .setSessionCookieNeeded(true)
                .setStreamBytesLimit(512 * 1024) // 512K
                .setHttpMessageCacheSize(1000)
                .setDisconnectDelay(30 * 1000);
    }


}